package com.dwsoft.service;

import org.springframework.beans.factory.annotation.Value;
import word.w2004.Document2004;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author sqw
 * @version 1.0
 * @description TODO
 * @ClassName IWordService
 * @Date 2020/8/5
 * @since jdk1.8
 */
public abstract class IWordService {

    @Value("${user.home}")
    private String sysUserHome;

    private static final String FILE_DIR="wordTmpFile";

    ThreadLocal<Document2004> document2004Local = new ThreadLocal<>();

    /**
     *功能描述 带参数
     * @author sqw
     * @return boolean
     * @throws
     * @date 2020/8/5
     */
    public abstract String generateWord(String id,Map<String,String> params) throws Exception;

    /**
     *功能描述 不带参数
     * @author sqw
     * @return boolean
     * @throws
     * @date 2020/8/5
     */
    public abstract String generateWord(String id) throws Exception;

    public String saveFile() throws IOException {
        String id =this.document2004Local.get().getId();
        String fileName=id+System.currentTimeMillis()+".doc";
        PrintWriter writer = null;
        String wordContent = this.document2004Local.get().getContent();
        try {
            File fileObj = new File(sysUserHome+"/"+FILE_DIR+"/"+fileName);
            if(!fileObj.exists()){
                fileObj.getParentFile().mkdirs();
                fileObj.createNewFile();
            }
            writer = new PrintWriter(fileObj);
            writer.println(wordContent);
            return fileName;
        }catch (Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            writer.close();
        }
    }

}
